package com.wencai.util;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangpan
 * @date 2023/5/9
 */
public class ExchangeDay {
    private static String holidayFilePath = "D:\\Users\\Administrator\\Desktop\\记录\\2023年节假日.txt";
    private static String tradeDaysFilePath = "D:\\Users\\Administrator\\Desktop\\记录\\trade_datas.txt";
    private static List<String> holidays = new ArrayList<>();
    private static Map<String, Boolean> dateIsTradeDayMap = new HashMap<>();

    public static void main(String[] args) throws IOException {
        String startDay = "20220101";
        String next = startDay;
        while (next.compareTo("20221231") < 0) {
            next = getNextExchangeDay(next);
            if (isExchangeDay(next)) {
                System.out.println(next);
            }
        }
    }

    static {
        try {
            holidays = IOUtils.readFromFile(holidayFilePath);
            List<String> tradeDateList = IOUtils.readFromFile(tradeDaysFilePath);
            for (String str : tradeDateList) {
                String[] split = str.split(",");
                dateIsTradeDayMap.put(DateConverter.convertToRaw(split[0].trim()),
                        split[1].trim().equals("1") ? true : false);
            }
        } catch (IOException e) {

        }
    }

    public static boolean isExchangeDay(String day) {
        if (day.startsWith("2023")) {
            LocalDate localDate = getLocalDate(day);
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            if (DayOfWeek.SATURDAY.equals(dayOfWeek) || DayOfWeek.SUNDAY.equals(dayOfWeek)) {
                return false;
            }
            return !holidays.contains(day);
        }
        return dateIsTradeDayMap.get(day);
    }

    @NotNull
    private static LocalDate getLocalDate(String day) {
        day = day.replaceAll("-","");
        String year = day.substring(0, 4);
        String month = day.substring(4, 6);
        String date = day.substring(6, 8);
        return LocalDate.of(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(date));
    }

    public static String getNextExchangeDay(String day) {
        LocalDate localDate = getLocalDate(day);
        LocalDate nextLocalDate = localDate.plusDays(1);
        String nextDayStr = getDateStr(nextLocalDate);
        while (!isExchangeDay(nextDayStr)) {
            return getNextExchangeDay(nextDayStr);
        }
        return nextDayStr;
    }

    public static String getLastOneMonthDay(String day) {
        LocalDate localDate = getLocalDate(day);
        LocalDate nextLocalDate = localDate.minusMonths(1);
        return getDateStr(nextLocalDate);
    }


    public static String getLatestExchangeDay(String day) {
        if (isExchangeDay(day)) {
            return day;
        }
        return getNextExchangeDay(day);
    }

    public static String getPreExchangeDay(String day) {
        LocalDate localDate = getLocalDate(day);
        LocalDate nextLocalDate = localDate.minusDays(1);
        String nextDayStr = getDateStr(nextLocalDate);
        while (!isExchangeDay(nextDayStr)) {
            return getPreExchangeDay(nextDayStr);
        }
        return nextDayStr;
    }


    private static String getDateStr(LocalDate nextLocalDate) {
        int monthValue = nextLocalDate.getMonthValue();
        String month = monthValue < 10 ? "0" + monthValue : "" + monthValue;
        int dayValue = nextLocalDate.getDayOfMonth();
        String day = dayValue < 10 ? "0" + dayValue : "" + dayValue;
        return nextLocalDate.getYear() + month + day;
    }
}
